package it.polimi.travelDream.ejb.beans;

import java.util.List;

import it.polimi.travelDream.ejb.Cthulhu.Persistenza;
import it.polimi.travelDream.ejb.interfaces.PackageMgrRemote;
import it.polimi.travelDream.entities.Hotel;
import it.polimi.travelDream.entities.Package;
import it.polimi.travelDream.entities.Transportation;
import it.polimi.travelDream.entities.Excursion;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 * Session Bean implementation class PackageMgr
 */
@Stateless
@LocalBean
public class PackageMgr implements PackageMgrRemote {

	Package pack;
	Persistenza persist=new Persistenza();
	public EntityManagerFactory factory;
	public EntityManager em;
	
	
	public PackageMgr(){
	factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
	em=factory.createEntityManager();
	}
	
    public Package getPack() {
		return pack;
	}

	public void setPack(Package pack) {
		this.pack = pack;
	}

	 /**
		 * @author Filipazzi,Crisciullo
		 * This method is used only by the Employees
		 *This method adds a pack to the system
		 */

	@Override
	public Package addPack(String name) {
		
		
	    	System.out.println(name);
			pack=new Package(1, name);
			persist.persista(pack);
			return pack;
			
			
		}
	

	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 *This method adds a travel to the package.
		 *The parameter 'id' is the package id.
		 */
	public void addTransportation(Transportation transp, int id){

	
		System.out.println("prima del new package");
		Package pack = new Package();
		System.out.println("prima del getid");
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
  		em=factory.createEntityManager();
		pack = em.find(Package.class, id);
		
		System.out.println("prima delsettransasfp");
		pack.setTransportation(transp);
	
		
  		try{
			 if(!em.getTransaction().isActive()){
				 em.getTransaction().begin(); 
				System.out.println("transazione attivata ");
				
			 }	 
			 System.out.println("persisting1");
			 em.persist(pack);
			 
			  if (em.getTransaction().isActive()){
			        System.out.println("IS ACTIVE");
		
			    } else {
			        System.out.println("NO ACTIVE");
			    }
			  
		        em.getTransaction().commit();
			 
			 
			 System.out.println("persistato");
			 
			 		 
		}catch (Exception e){
		    e.printStackTrace();
		    em.getTransaction().rollback();
		    System.out.println("ECCEZIONE:\nERROR: " + e.getMessage());
		}
	     		
  		em.close();
  		factory.close();
	}
		
	
	
	 /**
	 * @author Filipazzi,Crisciullo
	 *  This method is used only by the Employees
	 *This method adds an hotel to the package.
	 *The parameter 'id' is the package id.
	 */
	
	
	public void addHotel(Hotel hotel, int id){
		
		Package pack = new Package();
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
  		em=factory.createEntityManager();
  		pack = em.find(Package.class, id);
		pack.setHotel(hotel);

  		try{
			 if(!em.getTransaction().isActive()){
				 em.getTransaction().begin(); 
				System.out.println("transazione attivata ");
				
			 }	 
			 System.out.println("persisting1");
			 em.persist(pack);
			 
			  if (em.getTransaction().isActive()){
			        System.out.println("IS ACTIVE");
		
			    } else {
			        System.out.println("NO ACTIVE");
			    }
			  
		        em.getTransaction().commit();
			 
			 
			 System.out.println("persistato");
			 
			 		 
		}catch (Exception e){
		    e.printStackTrace();
		    em.getTransaction().rollback();
		    System.out.println("ECCEZIONE:\nERROR: " + e.getMessage());
		}
	     		
  		em.close();
  		factory.close();
	}
	
	 /**
	 * @author Filipazzi,Crisciullo
	 *  This method is used only by the Employees
	 *This method adds an excursion to the package.
	 *The parameter 'id' is the package id.
	 */
	
	public void addExcursion(Excursion excursion, int id){
		
		Package pack = new Package();
		
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
  		em=factory.createEntityManager();
  		pack = em.find(Package.class, id);
		pack.setExcursion(excursion);

  		try{
			 if(!em.getTransaction().isActive()){
				 em.getTransaction().begin(); 
				System.out.println("transazione attivata ");
				
			 }	 
			 System.out.println("persisting1");
			 em.persist(pack);
			 
			  if (em.getTransaction().isActive()){
			        System.out.println("IS ACTIVE");
		
			    } else {
			        System.out.println("NO ACTIVE");
			    }
			  
		        em.getTransaction().commit();
			 
			 
			 System.out.println("persistato");
			 
			 		 
		}catch (Exception e){
		    e.printStackTrace();
		    em.getTransaction().rollback();
		    System.out.println("ECCEZIONE:\nERROR: " + e.getMessage());
		}
	     		
  		em.close();
  		factory.close();
	
	
	}
		
		
	 /**
	 * @author Filipazzi,Crisciullo
	 *  This method is used only by the Employees
	 * It deletes a package starting from it's id
	 */
	
	@Override
	public void delPack(int idPackage) {
    	factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		Package p = em.find(Package.class, idPackage);
		em.getTransaction().begin();
		em.remove(p);
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 * It deletes an hotel from the package
		 */
	@Override
	public boolean delHotel(int id) {
		List<Package> lista=showAllPackages();
		for (int i=0; i<lista.size(); i++){
			List<Hotel> hotellist = lista.get(i).getHotel();
			for (int j=0; j<hotellist.size(); j++){
				if (id==hotellist.get(j).getId()){
					return true;
				}
			}
		}
		
		
		return false;
		
	}

	@Override
	public void addHotel() {
		// TODO Auto-generated method stub
		
	}

	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 * It deletes an excursion from the package
		 */
	@Override
	public boolean delExcursion(int id){
		
		List<Package> lista=showAllPackages();
		for (int i=0; i<lista.size(); i++){
			List<Excursion> exclist = lista.get(i).getExcursion();
			for (int j=0; j<exclist.size(); j++){
				if (id==exclist.get(j).getId()){
					return true;
				}
			}
		}
		
		
		return false;
		
	
	}

	@Override
	public void addDeparture() {
		// TODO Auto-generated method stub
		
	}
	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 * It deletes a departure from the package
		 */
	@Override
	public boolean delDeparture(int id) {
		List<Package> lista=showAllPackages();
		for (int i=0; i<lista.size(); i++){
			List<Transportation> exclist = lista.get(i).getTransportation();
			for (int j=0; j<exclist.size(); j++){
				if (id==exclist.get(j).getid()){
					return true;
				}
			}
		}
		
		
		return false;
		
	}

	@Override
	public void addReturn() {
		// TODO Auto-generated method stub
		
	}
	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 * It deletes a return from the package
		 */
	@Override
	public boolean delReturn(int id) {
		List<Package> lista=showAllPackages();
		for (int i=0; i<lista.size(); i++){
			List<Transportation> exclist = lista.get(i).getTransportation();
			for (int j=0; j<exclist.size(); j++){
				if (id==exclist.get(j).getid()){
					return true;
				}
			}
		}
		
		
		return false;
	}
	
	public Package getPackage(int id){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
		em=factory.createEntityManager();
		Package pack = em.find(Package.class, id);
		em.close();
		factory.close();
		return pack;
	}

	 /**
		 * @author Filipazzi,Crisciullo
		 *  This method is used only by the Employees
		 * It shows all the packages added by the Employees to the system
		 */
	
	public List<Package> showAllPackages(){
		factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
	em=factory.createEntityManager();

		TypedQuery<Package> query = em.createQuery("SELECT p FROM Package p", Package.class);

		List<Package> packlist = query.getResultList();

		em.close();
		factory.close();
		return packlist;
	}
	

	
	
	
	
	
}
